#ifndef __CUST_TASK_H__
#define __CUST_TASK_H__

#include "cust_timer.h"
enum
{
	TASK_STATE_SLEEP,
	TASK_STATE_WORK,
	TASK_INITIALIZE = 0,

	TASK_ID_SX127X = 0,
	TASK_ID_LORA_NODE,
	TASK_ID_LORA_SINGLE_GATEWAY,
	TASK_ID_LORA_MULT_GATEWAY,
	TASK_ID_USER,
	TASK_ID_MAX,
	TASK_ID_NONE = 0xff,

	SIGNAL_NONE = 0,
	SIGNAL_ANY,
	SIGNAL_TASK_TIMEOUT,
	SIGNAL_BASE = 0x1000,
	SIGNAL_SX127X_SPI_DONE,
	SIGNAL_SX127X_CHIP_POWER_ON,
	SIGNAL_SX127X_CHIP_POWER_DOWN,
	SIGNAL_SX127X_INIT_MODE,
	SIGNAL_SX127X_READY,
	SIGNAL_SX127X_TX,
	SIGNAL_SX127X_TX_ONCE,
	SIGNAL_SX127X_TX_ONCE_NO_ACK,
	SIGNAL_SX127X_TX_REPEAT,
	SIGNAL_SX127X_RX,
	SIGNAL_SX127X_RX_NO_ACK,
	SIGNAL_SX127X_RX_ONCE,
	SIGNAL_SX127X_RX_ONCE_NO_ACK,
	SIGNAL_SX127X_IRQ,
	SIGNAL_SX127X_NEW_DATA,
	SIGNAL_SX127X_TX_RESULT,
	SIGNAL_SX127X_RESET,
	SIGNAL_UART_RX_DONE,
	SIGNAL_UART_TX_REQ,
	SIGNAL_UART_TX_DONE,
};

typedef struct
{
	PV_Union Param;
	uint16_t ParamLen;
	uint16_t FromTaskID;
	uint16_t ToTaskID;
	uint16_t Signal;
}Signal_InfoStruct;
typedef void (*MyTaskFun_t)(Signal_InfoStruct *Signal);
typedef struct
{
	Loop_Buffer SignalQueue;
	Timer_t Timer;
	MyTaskFun_t Fun;
	__IO uint16_t WakeupSignal;
	__IO uint8_t TaskState;
	__IO uint8_t IsTimerWakeup;
}Task_InfoStruct;

typedef struct
{
	uint16_t Signal;
	MyTaskFun_t Fun;
}Signal_AnalyzeStruct;

void Task_Init(void);
void Task_Register(uint8_t TaskID, MyTaskFun_t Fun, Signal_InfoStruct *SignalBuf, uint8_t SignalBufLen);
void Task_Wait(uint8_t TaskID, uint16_t Signal, uint32_t To);
void Task_SendOnlySignal(uint8_t FromTaskID, uint8_t ToTaskID, uint16_t Signal);
void Task_SendSignal(uint8_t FromTaskID, uint8_t ToTaskID, Signal_InfoStruct *Signal);
void Task_Run(void);
void Task_ResetSignalQueue(uint8_t TaskID);
#endif
